import express =  require('express')
import bodyParser = require('body-parser')
import { Contact } from 'wechaty'
import './app'

const app = express()

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

app.all('/sendMsg', async(req, response) => {
    let reqObj = /post/i.test(req.method) ? req.body : req.query
    let { name, content } = reqObj
    let contact = await Contact.find({ name: new RegExp(name, 'i') })
    if (contact) {
        await contact.say(content)
        return response.send('发送成功')
    }
    return response.send('查无此人')
})

app.listen(3000)